package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwdcT;
import cn.e4j.modular.crm.entity.GwdctT;
import cn.e4j.modular.crm.entity.GwdczT;
import cn.e4j.modular.crm.mapper.GwdcTMapper;
import cn.e4j.modular.crm.mapper.GwdczTMapper;
import cn.e4j.modular.system.entity.SysOrg;
import cn.e4j.modular.system.mapper.SysOrgMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 组合促销价格维护 服务实现类
 *
 * @author shixc
 * @since 2019-01-28
 */
@Service
public class Gwdc004ServiceImpl extends ServiceImpl<GwdczTMapper, GwdczT> {
    @Autowired
    private SysOrgMapper sysOrgMapper;

    @Autowired
    private GwdcTMapper gwdcTMapper;

    @Autowired
    public SysAuthorityService sysAuthorityService;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getListData(Page<Object> page, Dto dto) {
        dto.put("status", "0");
        if (!dto.getString("GWDC016_1").equals("")) {
            if (dto.getString("GWDC016_2").equals("")) {
                dto.put("status", "1");
            } else {
                dto.put("status", "3");
            }
        } else if (!dto.getString("GWDC016_2").equals("")) {
            dto.put("status", "2");
        }
        //客户权限
        dto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(dto));
        //部门权限
        dto.put("ORGS", sysAuthorityService.getOrgsAuthority(dto));
        //渠道权限
        dto.put("GWAES", sysAuthorityService.getGwaesAuthority(dto));
//        List<Map<String, Object>> listData = this.baseMapper.listPage(page, dto);
        List<Map<String, Object>> listData = InvokeUtil.invokeBeanMethod("gwdczTMapper,listPage",page,dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public Dto getGwdc001(Dto dto) {
        ShiroUser user = ShiroKit.getUser();
        Dto pDto = Dtos.newDto();
        Date date = new Date();
        pDto.put("GWDC001", "CXHD" + DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMddHHmmss));
        pDto.put("GWDC008", DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd));
        pDto.put("GWDC011", user.getID());
        pDto.put("GWDC011_NAME", user.getNAME());
        SysOrg sysOrg = sysOrgMapper.selectById(user.getORG_ID());
        pDto.put("GWDC012", sysOrg.getID());
        pDto.put("GWDC012_NAME", sysOrg.getNAME());
        return pDto;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGwdb(List<GwdcT> gwdcTList) {
        ShiroUser user = ShiroKit.getUser();
        List<Dto> list = new ArrayList<>();
        for (GwdcT gwdcT : gwdcTList) {
            Dto pDto = InvokeUtil.invokeBeanMethod("gwdczTMapper,getGwdb",user.getDBC(), gwdcT.getGWDC007(), gwdcT.getGWDC016(), gwdcT.getGWDC018(), gwdcT.getGWDC008());
            if (pDto != null) {
                list.add(pDto);
            } else {
                if (gwdcT.getGWDC007().equals("2")) {
                    Dto dto = this.baseMapper.getGwak010(user.getDBC(), gwdcT.getGWDC016());
                    if (dto != null) {
                        Dto sDto = InvokeUtil.invokeBeanMethod("gwdczTMapper,getGwdb",user.getDBC(), "1", dto.getString("GWAK010"), gwdcT.getGWDC018(), gwdcT.getGWDC008());
                        if (sDto != null) {
                            list.add(sDto);
                        }
                    }
                }
            }
        }
        return list;
    }

    @Transactional
    public Dto addData(List<GwdcT> gwdcTList, List<GwdczT> gwdczTList, List<GwdctT> gwdctTList) {
        Dto outDto = Dtos.newDto();
        outDto.setAppCode(200);
        ShiroUser user = ShiroKit.getUser();
        String gwdc001 = "";
        Date date = new Date();
        String GWDCTIME = DateUtils.formatDate(date, "HH:mm:ss");
        //增加新的明细
        for (GwdcT gwdc : gwdcTList) {
            gwdc001 = gwdc.getGWDC001();
            Dto dto = this.baseMapper.getGwdc(user.getDBC(), gwdc.getGWDC001(), gwdc.getGWDC016(), gwdc.getGWDC018());
            if (dto != null) {
                outDto.setAppCode(400);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return outDto;
            } else {
                gwdc.setGWDCENT(user.getDBC());
                gwdc.setGWDCACTI("N");
                gwdc.setGWDCTIME(GWDCTIME);
                gwdc.insert();
                GwdcT gwdcT = new GwdcT();
                gwdcT.setGWDC001(gwdc001);
                gwdcT.setGWDCTIME(GWDCTIME);
                gwdcT.setGWDCENT(user.getDBC());
                UpdateWrapper<GwdcT> gwdcTUpdateWrapper = new UpdateWrapper<>();
                gwdcTUpdateWrapper.eq("GWDCENT", gwdc.getGWDCENT());
                gwdcTUpdateWrapper.eq("GWDC001", gwdc001);
                gwdcT.update(gwdcTUpdateWrapper);
            }
        }
        for (GwdczT gwdczT : gwdczTList) {
            QueryWrapper<GwdczT> gwdczTUpdateWrapper = new QueryWrapper<>();
            gwdczTUpdateWrapper.eq("GWDCZENT", user.getDBC());
            gwdczTUpdateWrapper.eq("GWDCZ001", gwdc001);
            gwdczTUpdateWrapper.eq("GWDCZ002", gwdczT.getGWDCZ002());
            gwdczTUpdateWrapper.eq("GWDCZ004", gwdczT.getGWDCZ004());
            int count = this.baseMapper.selectList(gwdczTUpdateWrapper).size();
            if (count > 0) {
                outDto.setAppCode(401);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return outDto;
            } else {
                gwdczT.setGWDCZENT(user.getDBC());
                gwdczT.setGWDCZ001(gwdc001);
                gwdczT.insert();
            }
        }
        if (gwdctTList.size() > 0) {
            for (GwdctT gwdct : gwdctTList) {
                gwdct.setGWDCTENT(user.getDBC());
                gwdct.setGWDCT001(gwdc001);
                gwdct.insert();
            }
        }
        return outDto;
    }

    @Transactional
    public void delete(GwdcT gwdc) {
        UpdateWrapper<GwdcT> gwdcTUpdateWrapper = new UpdateWrapper<>();
        gwdcTUpdateWrapper.eq("GWDCENT", gwdc.getGWDCENT());
        gwdcTUpdateWrapper.eq("GWDC001", gwdc.getGWDC001());
        gwdc.delete(gwdcTUpdateWrapper);
        QueryWrapper<GwdczT> gwdczTUpdateWrapper = new QueryWrapper<>();
        gwdczTUpdateWrapper.eq("GWDCZENT", gwdc.getGWDCENT());
        gwdczTUpdateWrapper.eq("GWDCZ001", gwdc.getGWDC001());
        int count = gwdcTMapper.selectList(gwdcTUpdateWrapper).size();
        if (count > 0) {
            GwdczT gwdczT = new GwdczT();
            UpdateWrapper<GwdczT> gwdczUpdateWrapper = new UpdateWrapper<>();
            gwdczUpdateWrapper.eq("GWDCZENT", gwdc.getGWDCENT());
            gwdczUpdateWrapper.eq("GWDCZ001", gwdc.getGWDC001());
            gwdczT.delete(gwdczUpdateWrapper);
        }
    }

    @Transactional
    public void delDataBatch(List<GwdcT> gwdcTList) {
        for (GwdcT gwdc : gwdcTList) {
            UpdateWrapper<GwdcT> gwdcTUpdateWrapper = new UpdateWrapper<>();
            gwdcTUpdateWrapper.eq("GWDCENT", gwdc.getGWDCENT());
            gwdcTUpdateWrapper.eq("GWDC001", gwdc.getGWDC001());
            gwdc.delete(gwdcTUpdateWrapper);
            QueryWrapper<GwdczT> gwdczTUpdateWrapper = new QueryWrapper<>();
            gwdczTUpdateWrapper.eq("GWDCZENT", gwdc.getGWDCENT());
            gwdczTUpdateWrapper.eq("GWDCZ001", gwdc.getGWDC001());
            int count = this.baseMapper.selectList(gwdczTUpdateWrapper).size();
            if (count > 0) {
                GwdczT gwdczT = new GwdczT();
                UpdateWrapper<GwdczT> gwdczUpdateWrapper = new UpdateWrapper<>();
                gwdczUpdateWrapper.eq("GWDCZENT", gwdc.getGWDCENT());
                gwdczUpdateWrapper.eq("GWDCZ001", gwdc.getGWDC001());
                gwdczT.delete(gwdczUpdateWrapper);
            }
        }
    }

    @Transactional
    public void check(List<GwdcT> gwdcTList) {
        ShiroUser user = ShiroKit.getUser();
        for (GwdcT gwdc : gwdcTList) {
            UpdateWrapper<GwdcT> gwdcTUpdateWrapper = new UpdateWrapper<>();
            gwdcTUpdateWrapper.eq("GWDCENT", gwdc.getGWDCENT());
            gwdcTUpdateWrapper.eq("GWDC001", gwdc.getGWDC001());
            gwdc.setGWDCACTI("Y");
            gwdc.setGWDC013(user.getID());
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMdd");
            gwdc.setGWDC014(fmt.format(LocalDate.now()));
            gwdc.update(gwdcTUpdateWrapper);
        }
    }

    @Transactional
    public void unCheck(List<GwdcT> gwdcTList) {
        for (GwdcT gwdc : gwdcTList) {
            UpdateWrapper<GwdcT> gwdcTUpdateWrapper = new UpdateWrapper<>();
            gwdcTUpdateWrapper.eq("GWDCENT", gwdc.getGWDCENT());
            gwdcTUpdateWrapper.eq("GWDC001", gwdc.getGWDC001());
            gwdc.setGWDCACTI("N");
            gwdc.setGWDC013("");
            gwdc.setGWDC014("");
            gwdc.update(gwdcTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listGwdc(Dto dto) {
        List<Map<String, Object>> gwcsDtos = this.baseMapper.listGwdc(dto);
        return gwcsDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listGwdcz(Dto dto) {
        List<Map<String, Object>> gwcsDtos = this.baseMapper.listGwdcz(dto);
        return gwcsDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> listGwdct(Dto dto) {
        List<Map<String, Object>> gwdctDtos = this.baseMapper.listGwdct(dto);
        return gwdctDtos;
    }

    @Transactional
    public Dto editData(List<GwdcT> gwdcTList, List<GwdczT> gwdczTList, List<GwdctT> gwdctTList) {
        ShiroUser user = ShiroKit.getUser();
        Dto outDto = Dtos.newDto();
        outDto.setAppCode(200);
        Date date = new Date();
        String GWDCTIME = DateUtils.formatDate(date, "HH:mm:ss");
        //增加新的明细
        for (GwdcT gwdc : gwdcTList) {
            UpdateWrapper<GwdcT> gwdcTUpdateWrapper = new UpdateWrapper<>();
            gwdcTUpdateWrapper.eq("GWDCENT", gwdc.getGWDCENT());
            gwdcTUpdateWrapper.eq("GWDC001", gwdc.getGWDC001());
            gwdcTUpdateWrapper.eq("GWDC002", gwdc.getGWDC002());
            gwdc.delete(gwdcTUpdateWrapper);
            QueryWrapper<GwdczT> gwdczUpdateWrapper = new QueryWrapper<>();
            gwdczUpdateWrapper.eq("GWDCZENT", gwdc.getGWDCENT());
            gwdczUpdateWrapper.eq("GWDCZ001", gwdc.getGWDC001());
            int count1 = this.baseMapper.selectList(gwdczUpdateWrapper).size();
            if (count1 > 0) {
                GwdczT gwdczT = new GwdczT();
                UpdateWrapper<GwdczT> gwdcz1UpdateWrapper = new UpdateWrapper<>();
                gwdcz1UpdateWrapper.eq("GWDCZENT", gwdc.getGWDCENT());
                gwdcz1UpdateWrapper.eq("GWDCZ001", gwdc.getGWDC001());
                gwdczT.delete(gwdcz1UpdateWrapper);

                GwdctT gwdctT = new GwdctT();
                UpdateWrapper<GwdctT> gwdctUpdateWrapper = new UpdateWrapper<>();
                gwdctUpdateWrapper.eq("GWDCTENT", gwdc.getGWDCENT());
                gwdctUpdateWrapper.eq("GWDCT001", gwdc.getGWDC001());
                gwdctT.delete(gwdctUpdateWrapper);
            }
            Dto dto = this.baseMapper.getGwdc(user.getDBC(), gwdc.getGWDC001(), gwdc.getGWDC016(), gwdc.getGWDC018());
            if (dto != null) {
                outDto.setAppCode(400);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return outDto;
            } else {
                gwdc.setGWDCENT(user.getDBC());
                gwdc.setGWDCACTI("N");
                gwdc.setGWDCTIME(GWDCTIME);
                gwdc.insert();
                for (GwdczT gwdczT : gwdczTList) {
                    QueryWrapper<GwdczT> gwdczTUpdateWrapper = new QueryWrapper<>();
                    gwdczTUpdateWrapper.eq("GWDCZENT", user.getDBC());
                    gwdczTUpdateWrapper.eq("GWDCZ001", gwdczT.getGWDCZ001());
                    gwdczTUpdateWrapper.eq("GWDCZ002", gwdczT.getGWDCZ002());
                    gwdczTUpdateWrapper.eq("GWDCZ004", gwdczT.getGWDCZ004());
                    int count = this.baseMapper.selectList(gwdczTUpdateWrapper).size();
                    if (count > 0) {
                        outDto.setAppCode(401);
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return outDto;
                    } else {
                        gwdczT.setGWDCZENT(user.getDBC());
                        gwdczT.setGWDCZ001(gwdc.getGWDC001());
                        gwdczT.insert();
                    }
                }
                for (GwdctT gwdct : gwdctTList) {
                    gwdct.setGWDCTENT(user.getDBC());
                    gwdct.setGWDCT001(gwdc.getGWDC001());
                    gwdct.insert();
                }
            }
        }
        return outDto;
    }

    @Transactional
    public void edit(GwdcT gwdcT) {
        UpdateWrapper<GwdcT> gwdcTUpdateWrapper = new UpdateWrapper<>();
        gwdcTUpdateWrapper.eq("GWDCENT", gwdcT.getGWDCENT());
        gwdcTUpdateWrapper.eq("GWDC001", gwdcT.getGWDC001());
        gwdcT.update(gwdcTUpdateWrapper);
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwdc001Data(Page<Object> page, Dto dto) {
//        List<Map<String, Object>> listData = this.baseMapper.getGwdc001Data(page, dto);
        List<Map<String, Object>> listData = InvokeUtil.invokeBeanMethod("gwdczTMapper,getGwdc001Data",page,dto);
        return listData;
    }

}
